package com.everbest.admin.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @Author : MSI-NB
 * @Desc :
 * @Date : 2020/5/26 14:33
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {


    @Value("${server.port}")
    private String serverPort;

    @Value("${server.servlet.context-path}")
    private String contextPath;


    @Autowired
    RedisTemplate redisTemplate;


    // 字节数组读取硬盘图片 输出字节流
    @RequestMapping("/readFile")
    public void readFile(HttpServletResponse response) {

        FileInputStream inputStream = null;
        OutputStream outputStream = null;

        String filePath = "D:\\file\\QrCodeImg\\test.png";

        try {
            File file = new File(filePath);
            if (!file.exists()) {
                throw new FileNotFoundException(filePath);
            }

            inputStream = new FileInputStream(file);
            outputStream = response.getOutputStream();

            byte[] bytes = new byte[2048];
            int length;

            // 或者使用 (length = inputStream.read(bytes)) != -1
            // 等于-1时，表示已经到了文件结尾，停止读取
            while ((length = inputStream.read(bytes)) > 0) {
                outputStream.write(bytes, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 字节缓冲流 读取文件 并输出
    @RequestMapping("/readFileBuffer")
    public void readFileBuffer(HttpServletResponse response) {

        BufferedInputStream inputStream = null;
        BufferedOutputStream outputStream = null;

        String filePath = "D:\\file\\QrCodeImg\\test.png";

        try {
            File file = new File(filePath);
            if (!file.exists()) {
                throw new FileNotFoundException(filePath);
            }

            inputStream = new BufferedInputStream(new FileInputStream(file));
            outputStream = new BufferedOutputStream(response.getOutputStream());

            byte[] bytes = new byte[2048];
            int length;

            //  或者使用 (length = inputStream.read(bytes)) >0
            // 等于-1时，表示已经到了文件结尾，停止读取
            while ((length = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    // 消息通知测试
    @PostMapping("/api/msg")
    public String test() {

        return null;
    }


}

